//
//  SAGlobalSettingsLabelCell.m
//  saasRetail
//
//  Created by ice on 2025/6/30.
//

#import "SAGlobalSettingsLabelCell.h"
#import "SALabelSettingModel.h"

@interface SAGlobalSettingsLabelCell()

@property (nonatomic, strong) UILabel *titleLab;
@property (nonatomic, strong) UITextField *numberTextField;
@property (nonatomic, strong) UIView *lineView;
@property (nonatomic, strong) UISwitch *mySwitch;

@end

@implementation SAGlobalSettingsLabelCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self initUI];
    }
    return self;
}

-(void)setModel:(SALabelSettingModel *)model {
    _model = model;
    self.titleLab.text = model.titleStr;
    if ([model.type isEqualToString:@"1"]) {
        self.mySwitch.on = [model.content isEqualToString:@"1"];
    } else {
        self.numberTextField.text = model.content;
    }
    if ([model.type isEqualToString:@"1"]) {
        self.mySwitch.hidden = NO;
        self.numberTextField.hidden = YES;
    } else {
        self.mySwitch.hidden = YES;
        self.numberTextField.hidden = NO;
    }
}

- (void)switchAction:(UISwitch *) s1 {
    if (s1.on == YES) {
        !self.textBlock ?: self.textBlock(@"1");
    }else{
        !self.textBlock ?: self.textBlock(@"0");
    }
}

#pragma mark - initUI

- (void)initUI {

    self.contentView.backgroundColor = [UIColor whiteColor];
    [self.contentView addSubview:self.titleLab];
    [self.contentView addSubview:self.numberTextField];
    [self.contentView addSubview:self.mySwitch];
    [self.contentView addSubview:self.lineView];
    self.mySwitch.hidden = YES;
    
    [self.titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.mas_equalTo(0);
        make.left.mas_equalTo(25);
        make.right.mas_equalTo(self.numberTextField.mas_left).offset(-15);
    }];
    
    [self.numberTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.mas_equalTo(0);
        make.right.mas_equalTo(-25);
        make.width.mas_equalTo(80);
    }];
    
    [self.mySwitch mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-25);
        make.centerY.mas_equalTo(0);
    }];
        
    [self.lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.right.mas_equalTo(0);
        make.height.mas_equalTo(1);
    }];
}

#pragma mark - UITextFieldDelegate

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    // 只允许数字输入
    NSCharacterSet *numbersOnly = [NSCharacterSet characterSetWithCharactersInString:@"0123456789"];
    NSCharacterSet *characterSetFromTextField = [NSCharacterSet characterSetWithCharactersInString:string];
    
    if (![numbersOnly isSupersetOfSet:characterSetFromTextField]) {
        return NO;
    }
    
    // 获取将要显示的文本
    NSString *newText = [textField.text stringByReplacingCharactersInRange:range withString:string];
    
    // 限制最多2位数字
    if (newText.length > 2) {
        return NO;
    }
    
    // 不允许以0开头的多位数
    if (newText.length > 1 && [newText hasPrefix:@"0"]) {
        return NO;
    }
    
    return YES;
}

- (void)textFieldDidEndEditing:(UITextField *)textField {
    // 更新模型中的content
    if (textField.text.length == 0) {
        textField.text = @"0";
    }
    if (self.model) {
        //确保一定是数字
        NSString *content = [NSString stringWithFormat:@"%zd",textField.text.integerValue];
        self.model.content = content;
        !self.textBlock ?: self.textBlock(content);
    }
}

#pragma mark - lazy init

- (UILabel *)titleLab {
    if (!_titleLab) {
        _titleLab = [[UILabel alloc] init];
        _titleLab.font = [UIFont systemFontOfSize:16];
        _titleLab.textColor = kColor(51, 51, 51, 1);
        _titleLab.text = @"天空";
    }
    return _titleLab;
}

- (UITextField *)numberTextField {
    if (!_numberTextField) {
        _numberTextField = [[UITextField alloc] init];
        _numberTextField.font = [UIFont systemFontOfSize:16];
        _numberTextField.textColor = kColor(51, 51, 51, 1);
        _numberTextField.textAlignment = NSTextAlignmentRight;
//        _numberTextField.borderStyle = UITextBorderStyleRoundedRect;
        _numberTextField.keyboardType = UIKeyboardTypeNumberPad;
        _numberTextField.placeholder = @"请输入";
        _numberTextField.delegate = self;
        _numberTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
    }
    return _numberTextField;
}

- (UIView *)lineView {
    if (!_lineView) {
        _lineView = [[UIView alloc] init];
        _lineView.backgroundColor = kLineColor;
    }
    return _lineView;
}

-(UISwitch *)mySwitch {
    if (!_mySwitch) {
        _mySwitch = [[UISwitch alloc] initWithFrame:CGRectZero];
        [_mySwitch setOnTintColor:kGreenColor];
        [_mySwitch addTarget:self action:@selector(switchAction:) forControlEvents:UIControlEventValueChanged];
    }
    return _mySwitch;
}

@end

